package com.hexb.smh.config;

import com.hexb.core.utils.Assert;
import com.hexb.smh.entity.Account;
import com.hexb.smh.errors.ErrorCodes;
import com.hexb.smh.utils.AccountHelper;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : hexb
 */
@Component
public class ActivatedInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        if("/management/verificationCode".equalsIgnoreCase(uri)) return true;
        if("/error".equalsIgnoreCase(uri)) return true;
        try {
            Account account = AccountHelper.getAccount();
            Assert.isFalse(account.getActivated(), ErrorCodes.RESET_PASSWORD_FIRST);
        } catch (UnauthenticatedException e) {
            return true;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
